﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;

namespace Acms.SqlDal
{
    public class TopicGroupDal : Base
    {
        public enum Fields
        {
            Id,
            SiteId,
            Name,
            Path,
            Notes,
            TopicCount,
            Created
        }

        protected override void ReadRecord(System.Collections.Hashtable table, System.Data.SqlClient.SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.SiteId, reader, table);
            GetReaderValue<string>(Fields.Name, reader, table);
            GetReaderValue<string>(Fields.Path, reader, table);
            GetReaderValue<string>(Fields.Notes, reader, table);
            GetReaderValue<int>(Fields.TopicCount, reader, table);
            GetReaderValue<DateTime>(Fields.Created, reader, table);
        }


        public Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);

            return base.ExecuteGetItem(cmd);
        }

        public Hashtable GetBySiteIdAndPath(int siteId, string path)
        {
            SqlCommand cmd = CreateCommand("GetBySiteIdAndPath");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "Path", path, SqlDbType.VarChar, 50);
            return base.ExecuteGetItem(cmd);
        }

        public Hashtable[] GetAllBySiteId(int siteId)
        {
            SqlCommand cmd = CreateCommand("GetAllBySiteId");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);

            return base.ExecuteGetCollection(cmd);
        }

        public void Update(ref int id, int siteId, string name, string path, string notes, DateTime created, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "Name", name, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "Path", path, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "Notes", notes, SqlDbType.VarChar, 1000);
            AddInputParameter(cmd, "Created", created, SqlDbType.DateTime);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "ConcurencyIdOut", SqlDbType.Timestamp);
            base.ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "ConcurencyIdOut");
        }

        public void Delete(int id, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            ExecuteCommand(cmd);
        }


    }
}
